package com.mdn.steelcase.service.impl;

import java.util.List;

import com.mdn.steelcase.dao.GenericDao;
import com.mdn.steelcase.exception.ReflectionException;
import com.mdn.steelcase.helper.ReflectionHelper;
import com.mdn.steelcase.service.GenericService;

public class GenericServiceImpl<T> implements GenericService<T> {

	protected GenericDao<T> dao;

	public GenericServiceImpl(GenericDao<T> genericDao) {
		this.dao = genericDao;
	}
	 
	@Override
	public T save(T instance) {
		return dao.save(instance);
	}
	
	@Override
	public T update(T instance) {
		return dao.update(instance);
	}

	@Override
	public T updateField(T instance, String fieldName, Object value) {
		try{
			ReflectionHelper.executeSetMethod(instance, fieldName, value);
			return update(instance);
		}catch(ReflectionException e){
			return null;
		}
	}

	@Override
	public void delete(Long id) {
		dao.delete(id);
	}

	@Override
	public void delete(T instance) {
		dao.delete(instance);
	}

	@Override
	public List<T> getAll() {
		return dao.getAll();
	}

	@Override
	public T get(Long id) {
		return dao.get(id);
	}

	@Override
	public boolean exist(Long id) {
		return dao.exist(id);
	}

	@Override
	public T findSingleByColumn(String columnName, Object value) {
		return dao.findSingleByColumn(columnName, value);
	}

	@Override
	public List<T> findByColumn(String columnName, Object value) {
		return dao.findByColumn(columnName, value);
	}
	
	@Override
	public T findSingleByExample(T example, String... params) throws ReflectionException {
		return dao.findSingleByExample(example, params);
	}

	@Override
	public List<T> findByExample(T example, String... params) throws ReflectionException {
		return dao.findByExample(example, params);
	}
}
